/*
** Copyright (C) 2011 Viens Yann
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <QLabel>
#include <QPushButton>
#include <sstream>
#include "ScoreDisplayer.hpp"

ScoreDisplayer::ScoreDisplayer(QWidget* parent, std::string const& gameName, std::list<std::pair<std::string, int> > const& score): QDialog(parent), _gameName(gameName), _score(score)
{
	this->setWindowTitle(("Meilleurs scores - " + _gameName).data());
	this->setFixedSize(320, 320);
	this->setVisible(true);
	this->show();
	displayScore();
}

ScoreDisplayer::~ScoreDisplayer()
{
	delete _global;
	std::list<QObject*>::iterator			itBegin = _saved.begin();
	const std::list<QObject*>::iterator		itEnd = _saved.end();
	
	while (itBegin != itEnd)
	{
		delete *itBegin;
		++itBegin;
	}
	_saved.clear();
}

void		ScoreDisplayer::displayScore()
{
	QHBoxLayout			*item;
	QLabel				*nickNameLabel, *scoreLabel, *title;
	QPushButton			*close;
	std::stringstream	stream;

	_global = new QVBoxLayout;
	title = new QLabel(("Meilleurs scores - " + _gameName).data());
	title->setAlignment(Qt::AlignCenter);
	_global->addWidget(title);
	_saved.push_back(title);
	
	std::list<std::pair<std::string, int> >::const_iterator			itBegin = _score.begin();
	const std::list<std::pair<std::string, int> >::const_iterator	itEnd = _score.end();
	while (itBegin != itEnd)
	{
		item = new QHBoxLayout();
		nickNameLabel = new QLabel((*itBegin).first.data());
		nickNameLabel->setAlignment(Qt::AlignLeft);
		stream << (*itBegin).second;
		scoreLabel = new QLabel(stream.str().data());
		scoreLabel->setAlignment(Qt::AlignRight);
		stream.str("");
		item->addWidget(nickNameLabel);
		item->addWidget(scoreLabel);
		_saved.push_back(nickNameLabel);
		_saved.push_back(scoreLabel);		
		_global->addLayout(item);
		++itBegin;
	}
	close = new QPushButton("Fermer");
	connect(close, SIGNAL(clicked()), this, SLOT(close()));
	_global->addWidget(close, Qt::AlignRight);
	_saved.push_back(close);
	this->setLayout(_global);
}